/**
 * [ { title:'Title1', children:[{title:'aaa',url:''}] }, {
 * title:'Title2',children:[{title:'bbb',url:''}] } ]
 */
define(function(require, exports, module) {
	var $ = jQuery = require('jquery');
	
	/*
	$(window).on('hashchange',function(){
		//alert('f');
	});
	*/
	function LeftNav(iframeId, topULId, leftDivId, data) {
		
		var index = [];
		for ( var a in data) {
			index.push(a);
		}
		
		var appId = index[0];
		var appData = data[index[0]];
		var moduleIndex = [];
		for ( var a in appData.modules) {
			moduleIndex.push(a);
		}
		var menuItem = appData.modules[moduleIndex[0]].menus[0].name;
		if (location.hash != '') {
			var shortHash = location.hash.substring(1);
			var hashArray = shortHash.split(".");
			var appId = hashArray[0];
			menuItem = hashArray[1];
		}

		var ele = $("#" + leftDivId);
		ele.empty();
		var self = this;
		for ( var i = index.length - 1; i >= 0; i--) {
			var app = data[index[i]];
			var topItem = $("<a class='topMenuItem' href='javascript:void(0)' id='" + app.id + "'>" + app.title + "</a>").on("click", function() {
				// location.hash = this.id;
				// alert(this.id);
				self.showLeftMenu(ele, iframeId, this.id, null, data[this.id]);
				return false;
			});
			var li = $('<li>').append(topItem)
			$('#' + topULId).prepend(li);
		}
		
		this.showLeftMenu(ele, iframeId, appId, menuItem, data[appId]);

		/*
		 * for ( var i = 0; i < data.length; i++) { var m = data[i]; var module =
		 * $('<div>'); var moduleTitle = $('<div>').addClass("nav-left").addClass("nav-left-head");
		 * moduleTitle.append(m.title);
		 * 
		 * var op = $('<a href="javascript:void(0)">&nbsp;</a>').addClass("nav-left-head-op");
		 * 
		 * op.on('click', function() { self.titleMenuClick(this); });
		 * 
		 * op.addClass("nav-left-head-op-plus"); moduleTitle.append(op);
		 * module.append(moduleTitle); for ( var j = 0; j < m.children.length;
		 * j++) { var sValue = m.children[j]; var second = $('<div>').addClass("nav-left").addClass("nav-left-content");
		 * 
		 * var node = $("<a href='javascript:void(0)' url='" + sValue.url +
		 * "'>" + sValue.title + "</a>"); node.on('click', function() {
		 * self.menuClick(iframeId, this); }); second.append(node);
		 * module.append(second); if (i == 0 && j == 0) {
		 * second.addClass("nav-left-click"); node.click(); } }
		 * $(op).parent().nextAll().hide(); if (i == 0) {
		 * self.titleMenuClick(op); } ele.append(module); }
		 */
	}
	;

	LeftNav.prototype.showLeftMenu = function(ele, iframeId, appId, menuItem, appData) {
		ele.empty();
		var self = this;
		var modules = [];
		//alert(appData);
		for ( var m in appData.modules) {
			modules.push(m);
		}
		var ckModule = modules[0];
		if (menuItem != null) {
			for ( var i = 0; i < modules.length; i++) {
				var m = modules[i];
				var mdata = appData.modules[m].menus;
				for ( var j = 0; j < mdata.length; j++) {
					var sValue = mdata[j];
					if (sValue.name == menuItem) {
						ckModule = m;
						
					}
				}
			}
		}else {
			
			menuItem = appData.modules[modules[0]].menus[0].name;
		}
		
		//alert(ckModule+">ck>" + menuItem);
		var titleObj;
		var itemEl;
		var itemObj ;
		for ( var i = 0; i < modules.length; i++) {
			var m = modules[i];
			
			var mdata = appData.modules[m].menus;
			var module = $('<div>');
			var moduleTitle = $('<div>').addClass("nav-left").addClass("nav-left-head");
			moduleTitle.append(m);

			var op = $('<a href="javascript:void(0)">&nbsp;</a>').addClass("nav-left-head-op");

			op.on('click', function() {
				self.titleMenuClick(this);
			});

			op.addClass("nav-left-head-op-plus");
			moduleTitle.append(op);
			module.append(moduleTitle);

			for ( var j = 0; j < mdata.length; j++) {
				var sValue = mdata[j];
				var second = $('<div>').addClass("nav-left").addClass("nav-left-content");
				var loc = appId + '.' + sValue.name;
				var title = sValue.name;
				if(sValue.title) {
					title = sValue.title;
				}
				
				var node = $("<a href='javascript:void(0)' loc='" + loc + "' url='" + sValue.url + "'>" + title + "</a>");
				node.on('click', function() {
					self.menuClick(iframeId, this);
				});
				second.append(node);
				module.append(second);

				/*
				 * if (i == 0 && j == 0) { second.addClass("nav-left-click");
				 * node.click(); }
				 */
				//alert("name:" + sValue.name);
				if (menuItem == sValue.name) {
					// self.menuClick(iframeId, node);
					itemObj = node ;
					itemEl = 'a[loc="' + loc + '"]';
					//alert("el:" + itemEl)
				}
			}

			if (ckModule == m) {
				// alert('f');
				// self.titleMenuClick(op);
				titleObj = op;
			}

			$(op).parent().nextAll().hide();
			ele.append(module);
		}
		//alert(itemEl);
//		alert(titleObj);
		self.titleMenuClick(titleObj);
		self.menuClick(iframeId, itemObj);
		//alert("loc:" + itemEl);
		// itemObj.click();
	}

	LeftNav.prototype.titleMenuClick = function(obj) {
		obj = $(obj);
		if (obj.hasClass("nav-left-head-op-plus")) {
			$(obj).parent().nextAll().show();
			obj.removeClass("nav-left-head-op-plus");
			obj.addClass("nav-left-head-op-sub");
		} else {
			$(obj).parent().nextAll().hide();
			obj.removeClass("nav-left-head-op-sub");
			obj.addClass("nav-left-head-op-plus");
		}

	};

	LeftNav.prototype.menuClick = function(iframeId, obj) {
		var ele = $(obj);
		var mainDiv = ele.parent().parent().parent();
		var old = mainDiv.find('.nav-left-click');
		// alert(old.find('a').attr('url'));
		old.removeClass("nav-left-click");

		ele.parent().addClass("nav-left-click");
		location.hash = ele.attr("loc");
		document.getElementById(iframeId).src = ele.attr('url');
	};

	return function(iframeId, topULId, leftDivId, data) {
		return new LeftNav(iframeId, topULId, leftDivId, data);
	}
});